<template>
<view>
<!--hotmall/packageB/openShop/openShop.wxml-->
<view class="content">
   <view class="import">
      <text>店铺名称</text>
      <input type="text" @input="nameFun"></input>
   </view>
   <view class="import">
      <text>手机号码</text>
      <input type="number" @input="phoneFun"></input>
   </view>
   <view class="import">
      <text>主营类目</text>
      <input type="text" @input="leimuFun"></input>
   </view>
   <view class="section">
      <view class="section__title">所在地区</view>
      <picker mode="region" @change="bindRegionChange" :value="region" :custom-item="customItem">
         <view class="picker">
            {{region[0]}} {{region[1]}} {{region[2]}}
         </view>
      </picker>
   </view>
   <view class="import">
      <text>门店地址</text>
      <input type="text" @input="dizhiFun"></input>
   </view>
</view>
<button @tap="submits" hover-class="none">提交</button>
</view>
</template>

<script>
// hotmall/packageB/openShop/openShop.js
var app = getApp();
import api from "../../../api";

export default {
  data() {
    return {
      show: false,
      uid: '',
      name: '',
      phone: '',
      leimu: '',
      dizhi: '',
      region: ['北京市', '北京市', '东城区']
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getStorage({
      key: 'userId',
      success: res => {
        console.log(res);
        this.setData({
          uid: res.data
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    //获取输入的姓名,类目，地区
    nameFun(e) {
      console.log(e.detail.value);
      this.setData({
        name: e.detail.value
      });
    },

    phoneFun(e) {
      console.log(e.detail.value);
      this.setData({
        phone: e.detail.value
      });
    },

    leimuFun(e) {
      console.log(e.detail.value);
      this.setData({
        leimu: e.detail.value
      });
    },

    dizhiFun(e) {
      console.log(e.detail.value);
      this.setData({
        dizhi: e.detail.value
      });
    },

    //提交
    submits: function () {
      if (this.name == '') {
        wx.showToast({
          title: '请输入店铺名称',
          icon: 'none'
        });
      } else {
        if (this.leimu == '') {
          wx.showToast({
            title: '请输入主营类目',
            icon: 'none'
          });
        } else {
          if (this.dizhi == '') {
            wx.showToast({
              title: '请输入门店地址',
              icon: 'none'
            });
          } else {
            let TEL = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/,
                phone = this.phone;

            if (!TEL.test(phone)) {
              wx.showToast({
                title: '请输入正确的手机号码',
                icon: 'none'
              });
            } else {
              app.globalData.request({
                url: api.default.user_certification,
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  user_id: this.uid,
                  store_name: this.name,
                  phone: this.phone,
                  address: this.region[0] + this.region[1] + this.region[2] + this.dizhi,
                  category: this.leimu
                },
                method: 'POST',
                success: res => {
                  console.log(res);
                  this.setData({});

                  if (res.msg == "认证已提交,正等待管理员审核!") {
                    wx.showToast({
                      title: res.msg,
                      icon: 'none'
                    });
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      });
                    }, 1500);
                  } else {
                    wx.showToast({
                      title: res.msg,
                      icon: 'none'
                    });
                  }
                }
              });
            }
          }
        }
      }
    },

    //弹出地址
    onClose() {
      this.setData({
        show: false
      });
    },

    bindRegionChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value);
      console.log(e.detail.value[0] + e.detail.value[1]);
      this.setData({
        region: e.detail.value
      });
    }
  }
};
</script>
<style>
@import "./openShop.css";
</style>